import {ModalForm, ProFormSelect, ProFormText, ProFormTextArea} from "@ant-design/pro-components";
import {useRef} from "react";
import {ProFormInstance} from "@ant-design/pro-form/lib";
import {useSnapshot} from "@@/exports";
import {SettingModel} from "@/models/setting";
import {getNotifyList} from "@/services/NotifyService";


export interface SettingUpdateModalProps {
    onFinish: (formData: any) => Promise<boolean | void>;
}

const SettingUpdateModal = (props: SettingUpdateModalProps) => {
    let settingState = useSnapshot(SettingModel.state);
    let {actions: settingActions} = SettingModel
    const formRef = useRef<ProFormInstance>()
    return <>
        <ModalForm
            key={settingState.setting?.id || -1}
            formRef={formRef}
            title={settingState.setting ? "修改营销设置" : "添加营销设置"}
            open={settingState.updateModalState}
            initialValues={settingState.setting}
            onFinish={props.onFinish}
            onOpenChange={settingActions.setUpdateModalState}
        >
            <ProFormText
                label="id"
                hidden
                name="id"
            />

            <ProFormSelect
                label="客资通知"
                mode="multiple"
                name="customerNotify"
                rules={[{required: true, message: "请填写客资通知"}]}
                request={async () => {
                    let data = await getNotifyList({});
                    return data.map((d) => ({label: d.remark, value: d.id}))
                }}
            />
            <ProFormTextArea
                label="客资通知文案"
                name="customerNotifyText"
                rules={[{required: true, message: "客资通知文案"}]}
            />

            <ProFormSelect
                label="订单通知"
                mode="multiple"
                name="orderNotify"
                rules={[{required: true, message: "请填写订单通知"}]}
                request={async () => {
                    let data = await getNotifyList({});
                    return data.map((d) => ({label: d.remark, value: d.id}))
                }}
            />

            <ProFormTextArea
                label="订单通知文案"
                name="orderNotifyText"
                rules={[{required: true, message: "订单通知文案"}]}
            />
        </ModalForm>
    </>
}

export default SettingUpdateModal
